package com.treetao.union.web.controller.mall;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.treetao.union.db.model.MallPSku;
import com.treetao.union.db.model.MallPSpu;
import com.treetao.union.db.model.vo.LayerData;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.db.service.MallPSkuService;
import com.treetao.union.db.service.MallPSpuService;
import com.treetao.union.web.constant.Constants;
import com.treetao.union.web.controller.BaseController;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author Dev.M
 * @since 2020-04-11
 */
@Controller
@RequestMapping(Constants.AMIND_PREFIX + "/mall-psku")
public class MallPSkuController extends BaseController {

	@Autowired
	private MallPSkuService mallPSkuService;

	@Autowired
	private MallPSpuService mallPSpuService;

	@GetMapping("list")
	public String list() {
		return "/admin/mall-psku/list";
	}

	@PostMapping("list")
	@ResponseBody
	public LayerData<MallPSku> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, ServletRequest request) {
		Map map = WebUtils.getParametersStartingWith(request, "s_");
		LayerData<MallPSku> layerData = new LayerData<>();
		QueryWrapper<MallPSku> wrapper = new QueryWrapper<MallPSku>();
		if (!map.isEmpty()) {
			String delFlag = (String) map.get("delFlag");
			if (StringUtils.isNotBlank(delFlag)) {
				wrapper.eq("del_flag", Boolean.valueOf(delFlag));
			} else {
				map.remove("del_flag");
			}
			String isrec = (String) map.get("isrec");
			if (StringUtils.isNotBlank(isrec)) {
				wrapper.eq("isrec", Boolean.valueOf(isrec));
			} else {
				map.remove("isrec");
			}
			String skuno = (String) map.get("skuno");
			if (StringUtils.isNotBlank(skuno)) {
				wrapper.like("skuno", skuno);
			} else {
				map.remove("skuno");
			}

			String name = (String) map.get("name");
			if (StringUtils.isNotBlank(name)) {
				wrapper.like("name", name);
			} else {
				map.remove("name");
			}

		}
		IPage<MallPSku> pageData = mallPSkuService.page(new Page<>(page, limit), wrapper);
		layerData.setData(pageData.getRecords());
		layerData.setCount(pageData.getTotal());
		return layerData;
	}

	@GetMapping("add")
	public String add() {
		return "/admin/mall-psku/add";
	}

	@PostMapping("add")
	@ResponseBody
	public RestResponse add(MallPSku mallPSku) {
		if (StringUtils.isBlank(mallPSku.getSkuno())) {
			return RestResponse.failure("SKU编号不能为空");
		}
		if (StringUtils.isBlank(mallPSku.getName())) {
			return RestResponse.failure("SKU名称不能为空");
		}
		if (mallPSku.getPriceSale() == null) {
			return RestResponse.failure("销售价不能为空");
		}
		if (StringUtils.isBlank(mallPSku.getAttrItemIds())) {
			return RestResponse.failure("属性不能为空");
		}
		if (StringUtils.isBlank(mallPSku.getThumbnail())) {
			return RestResponse.failure("主图不能为空");
		}
		if (StringUtils.isBlank(mallPSku.getImgs())) {
			return RestResponse.failure("介绍图不能为空");
		}
		if (mallPSku.getIsrec() == null) {
			return RestResponse.failure("推荐不能为空");
		}
		
		QueryWrapper<MallPSku> q = getQueryWrapperNoDel();
		q.eq("skuno", mallPSku.getSkuno());
		int count = mallPSkuService.count(q);
		if (count > 0) {
			return RestResponse.failure("SKU编号重复");
		}
		
		boolean flag = mallPSkuService.persist(mallPSku);
		if (flag) {
			return RestResponse.success();
		}
		return RestResponse.failure("保存失败");
	}

	@GetMapping("detail")
	public String detail(Long id, Model model) {
		MallPSku mallPSku = mallPSkuService.getById(id);
		model.addAttribute("mallPSku", mallPSku);
		
		MallPSpu spu = mallPSpuService.getById(mallPSku.getSpuId());
		model.addAttribute("spuName", spu == null ? null : spu.getName());
		
		return "/admin/mall-psku/detail";
	}

	@GetMapping("edit")
	public String edit(Long id, Model model) {
		MallPSku mallPSku = mallPSkuService.getById(id);
		model.addAttribute("mallPSku", mallPSku);

		MallPSpu spu = mallPSpuService.getById(mallPSku.getSpuId());
		model.addAttribute("spuName", spu == null ? null : spu.getName());

		return "/admin/mall-psku/edit";
	}

	@PostMapping("edit")
	@ResponseBody
	public RestResponse edit(MallPSku mallPSku) {
		if (null == mallPSku.getId() || 0 == mallPSku.getId()) {
			return RestResponse.failure("ID不能为空");
		}
		if (StringUtils.isBlank(mallPSku.getSkuno())) {
			return RestResponse.failure("SKU编号不能为空");
		}
		if (StringUtils.isBlank(mallPSku.getName())) {
			return RestResponse.failure("SKU名称不能为空");
		}
		if (mallPSku.getPriceSale() == null) {
			return RestResponse.failure("销售价不能为空");
		}
		if (StringUtils.isBlank(mallPSku.getThumbnail())) {
			return RestResponse.failure("主图不能为空");
		}
		if (StringUtils.isBlank(mallPSku.getImgs())) {
			return RestResponse.failure("介绍图不能为空");
		}
		if (mallPSku.getIsrec() == null) {
			return RestResponse.failure("推荐不能为空");
		}
		MallPSku sku = mallPSkuService.getById(mallPSku.getId());
		if (sku != null) {
			if (!sku.getSkuno().equals(mallPSku.getSkuno())) {
				QueryWrapper<MallPSku> q = getQueryWrapperNoDel();
				q.eq("skuno", mallPSku.getSkuno());
				int count = mallPSkuService.count(q);
				if (count > 0) {
					return RestResponse.failure("SKU编号重复");
				}
			}
		}
		boolean flag = mallPSkuService.modify(mallPSku);
		if (flag) {
			return RestResponse.success();
		}
		return RestResponse.failure("保存失败");
	}

	@PostMapping("batchDel")
	@ResponseBody
	public RestResponse batchDel(@RequestParam(value = "ids[]", required = false) List<Long> ids) {
		if (null == ids || ids.size() == 0) {
			return RestResponse.failure("请选择一项");
		}
		return RestResponse.success().setDataOnlyResult(mallPSkuService.removeByIds(ids));
	}

	@PostMapping("delete")
	@ResponseBody
	public RestResponse delete(@RequestParam(value = "id", required = false) Long id) {
		if (null == id || 0 == id) {
			return RestResponse.failure("ID不能为空");
		}
		return RestResponse.success().setDataOnlyResult(mallPSkuService.removeById(id));
	}

	@PostMapping("change-status")
	@ResponseBody
	public RestResponse change_status(@RequestParam(value = "id", required = true) Long id,
			@RequestParam(value = "status", required = false) Boolean status) {
		MallPSku mallPSku = mallPSkuService.getById(id);
		mallPSku.setDelFlag(status);
		mallPSkuService.updateById(mallPSku);
		return RestResponse.success();
	}

}